/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.ohos.speechutilsdemo.slice;

import com.ohos.speechutilsdemo.ListProvider;
import com.ohos.speechutilsdemo.ResourceTable;

import ee.ioc.phon.ohos.speechutils.utils.LogUtils;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.agp.window.service.WindowManager;
import ohos.global.resource.NotExistException;
import ohos.global.resource.ResourceManager;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * MainAbilitySlice
 *
 * @since 2021-03-20
 */
public class MainAbilitySlice extends AbilitySlice {
    private final String mainAbilitySliceTag = "test";

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS);
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_FULL_SCREEN);
        super.setUIContent(ResourceTable.Layout_ability_main);

        try {
            initListContainer();
        } catch (NotExistException | WrongTypeException | IOException e) {
            LogUtils.log(LogUtils.DEBUG, mainAbilitySliceTag, e.getMessage());
        }
    }

    private void initListContainer() throws NotExistException, WrongTypeException, IOException {
        ListContainer listContainer = (ListContainer) findComponentById(ResourceTable.Id_listText);
        List<String> list = new ArrayList<String>();
        ResourceManager resManager = getContext().getResourceManager();
        String speechToText = resManager.getElement(ResourceTable.String_SpeechToText).getString();
        String textToSpeech = resManager.getElement(ResourceTable.String_TextToSpeech).getString();
        list.add(speechToText);
        list.add(textToSpeech);
        ListProvider listProvider = new ListProvider(this, list);
        listContainer.setItemProvider(listProvider);
        listContainer.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int position, long id) {
                String item = (String) listContainer.getItemProvider().getItem(position);
                if (item.equals(getAbility().getString(ResourceTable.String_SpeechToText))) {
                    /**
                     * 语音转文字
                     */
                    AbilitySlice abilitySlice = new AsrAbilitySlice();
                    Intent intent1 = new Intent();
                    present(abilitySlice, intent1);
                } else {
                    /**
                     * 文字转语音
                     */
                    AbilitySlice abilitySlice = new TtsAbilitySlice();
                    Intent intent1 = new Intent();
                    present(abilitySlice, intent1);
                }
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
